/*
 * Created by: vadikv
 * Created: Monday, April 07, 2008
 */

using System.Collections.Generic;
using OneDayModel.Interfaces;
using OneDayModel.Model;

namespace OneDayModel.PresentationLogic
{
    public class ExistingUserProfileUILogic
    {
        private readonly IExistingUserProfileUI existingUserProfileUI;
        private readonly IExistingUserProfileModel existingUserProfileModel;

        public ExistingUserProfileUILogic(IExistingUserProfileUI existingUserProfileUI, IExistingUserProfileModel existingUserProfileModel)
        {
            this.existingUserProfileUI = existingUserProfileUI;
            this.existingUserProfileModel = existingUserProfileModel;
            existingUserProfileUI.SetAttributesDefinitions(existingUserProfileModel.AllAttributesDefinitions);
            RegisterEvents();
        }

        private void RegisterEvents()
        {
            existingUserProfileUI.onShowExistingUserProfile += existingUserProfileUI_onShowExistingUserProfile;
        }

        void existingUserProfileUI_onShowExistingUserProfile(object sender, ShowExistingUserProfileArgs e)
        {
            if (e.SessionContext.User == null)
            {
                ShowReadOnlyProfile(e.UserAlias, false);
            }
            else
            {
                ShowReadOnlyProfile(e.UserAlias, true);
            }
        }

        private void ShowReadOnlyProfile(string userAlias, bool enableChat)
        {
            ExistingUserProfileData existingUserProfileData = existingUserProfileModel.GetProfileData(userAlias);
            existingUserProfileUI.ShowRoles(existingUserProfileData.UserRoles);
            IEnumerable<KeyValuePair<string, string>> allAtributes = existingUserProfileData.GetAtributes;
            string excludeFromList = "eMail;Password;AccountStatus";
            List<KeyValuePair<string, string>> shownAttributes = new List<KeyValuePair<string, string>>();
            foreach (KeyValuePair<string, string> attribute in allAtributes)
            {
                if (!excludeFromList.Contains(attribute.Key))
                {
                    shownAttributes.Add(attribute);
                }
            }
            existingUserProfileUI.ShowAttributes(shownAttributes, true);
            existingUserProfileUI.EnableChat = enableChat;
        }
    }
}